<?php

namespace app\admin\model\deerhome;

use think\Model;


class ShopApply extends Model
{

    // 表名
    protected $name = 'deerhome_shop_apply';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    
    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $row->add_time=date('Y-m-d H:i:s');
            if(empty($row->mdname)){
                throw new \Exception(__('门店名称不能为空'));
            }
            if(empty($row->address)){
                throw new \Exception(__('请选择门店定位!'));
            }
            if(empty($row->latitude) || empty($row->longitude)){
                throw new \Exception(__('请选择门店定位'));
            }
            if(empty($row->user_name)){
                throw new \Exception(__('负责人姓名不能为空'));
            }
            if(empty($row->city) || $row->city=='请选择'){
                throw new \Exception(__('门店所在区域不能为空'));
            }
            $arrs=explode("-",$row->city);
            if(count($arrs)!=3){
                throw new \Exception(__('门店所在区域选择不完整'));
            }
            $row->province=$arrs[0];
            $row->city=$arrs[1];
            $row->area=$arrs[2];
            
            if(empty($row->user_tel) || !preg_match("/^1\d{10}$/",$row->user_tel)){
                throw new \Exception(__('手机号填写错误'));
            }
        });
    }
    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
